package cn.donghuapian.controller;

import cn.donghuapian.component.MinioService;
import cn.donghuapian.constant.CommonConstant;
import cn.donghuapian.entity.WeaponEntity;
import cn.donghuapian.service.impl.WeaponServiceImpl;
import cn.donghuapian.vo.ResponseBody;
import cn.donghuapian.vo.WeaponVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hzw
 * @since 2024-09-23
 */
@RestController
@RequestMapping("/weapon")
public class WeaponController {

    @Autowired
    private WeaponServiceImpl weaponService;

    @Autowired
    private MinioService minioService;

    @PostMapping("/page")
    public ResponseBody<Page<WeaponVO>> page(Page<WeaponEntity> param) {
        Page<WeaponEntity> page = weaponService.page(param);
        Page<WeaponVO> result = new Page<>();
        List<WeaponVO> records = new ArrayList<>();
        for(WeaponEntity entity : page.getRecords()) {
            WeaponVO vo = new WeaponVO();
            BeanUtils.copyProperties(entity, vo);
//            vo.setImageUrl(CommonConstant.IMAGE_URL + entity.getImageId());
            vo.setImageUrl(minioService.getUrl(vo.getImageName()));
            records.add(vo);
        }
        result.setRecords(records);
        return ResponseBody.success(result);
    }

    @PostMapping("/add")
    public String add(WeaponEntity entity) {
        weaponService.save(entity);
        return "成功";
    }
}
